package com.example.sh_crosses.game;

import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sh_crosses.R;
import com.example.sh_crosses.base.BaseActivity;

import java.util.ArrayList;
import java.util.Arrays;

public class GameActivity extends BaseActivity {
    private int[] gameState;
    int activePlayer = 1;
    boolean gameIsActive = true;
    int count = 0;
    ArrayList<int[]> winningPositions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        gameState = new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2};
        winningPositions = new ArrayList<>();
        winningPositions.add(new int[]{0, 1, 2});
        winningPositions.add(new int[]{3, 4, 5});
        winningPositions.add(new int[]{6, 7, 8});
        winningPositions.add(new int[]{0, 3, 6});
        winningPositions.add(new int[]{1, 4, 7});
        winningPositions.add(new int[]{2, 5, 8});
        winningPositions.add(new int[]{0, 4, 8});
        winningPositions.add(new int[]{2, 4, 6});

    }

    /**
     * 点击填充颜色
     *
     * @param view 按钮视图
     */
    public void dropIn(View view) {
        int de = 2;
        int max = 9;
        ImageView counter = (ImageView) view;
        TextView txt = findViewById(R.id.winner1);
        LinearLayout layout = findViewById(R.id.winner);
        int tappedcounter = Integer.parseInt(counter.getTag().toString());
        if(!gameIsActive){
            return;
        }
        if (gameState[tappedcounter] == de) {
            if (activePlayer == 1) {
                counter.setImageResource(R.drawable.circle1);
                activePlayer = 0;
                count++;
                gameState[tappedcounter] = 1;
            }else {
                counter.setImageResource(R.drawable.circle2);
                activePlayer = 1;
                count++;
                gameState[tappedcounter] = 0;
            }
        }else{
            return;
        }
        counter.setTranslationY(-1000f);
        counter.animate().translationYBy(1000f).rotationY(1800f).setDuration(1000);
        for (int[] winningposition : winningPositions) {
            if (gameState[winningposition[0]] == gameState[winningposition[1]] && gameState[winningposition[1]] == gameState[winningposition[2]] && gameState[winningposition[0]] != 2) {
                if (gameState[winningposition[0]] == 0) {
                    txt.setText(getResources().getString(R.string.red_winner));
                } else if (gameState[winningposition[0]] == 1) {
                    txt.setText(getResources().getString(R.string.green_winner));
                }
                layout.setVisibility(View.VISIBLE);
                gameIsActive = false;
            }
        }
        if (gameIsActive && count == max) {
            txt.setText(getResources().getString(R.string.draw));
            layout.setVisibility(View.VISIBLE);
            gameIsActive = false;
        }
    }

    /**
     * 再来一局
     *
     * @param view 重玩视图
     */
    public void playAgain(View view) {
        activePlayer = 1;
        gameIsActive = true;
        count = 0;
        LinearLayout linearLayout = findViewById(R.id.winner);
        GridLayout gridLayout = findViewById(R.id.gridLayout);
        Arrays.fill(gameState, 2);
        linearLayout.setVisibility(View.GONE);
        for (int i = 0; i < gridLayout.getChildCount(); i++) {
            ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_from_left);
    }
}
